package com.joker.registration.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joker.registration.hosp.service.impl.HospitalSetServiceImpl;
import exception.YyghException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import model.com.manubao.yygh.model.hosp.HospitalSet;
import model.com.manubao.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import result.Result;
import util.MD5;
import util.Note;

import java.util.List;
import java.util.Random;

/**
 * @author 20405
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetServiceImpl hospitalSetService;
    @PostMapping("findPageHospSet/{current}/{limit}")
    @ApiOperation(value = "获取所有医院设置并分页查询and分页")
    public Result findHosp(@PathVariable Long current,
                           @PathVariable Long limit,HospitalSetQueryVo searchObj){
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
//        String hosname = hospitalSetQueryVo.getHosname();//获取传入的医院名称并存放String后续判断
//        String hoscode = hospitalSetQueryVo.getHoscode();//获取传入的医院编号并存放String后续判断
//        if(!StringUtils.isEmpty(hosname)) {//当传入的hosname不为空
//            wrapper.like("hosname",hospitalSetQueryVo.getHosname());//条件构造器模糊查询
//        }
//        if(!StringUtils.isEmpty(hoscode)) {
//            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
//        }
//        wrapper.orderByDesc("create_time"); //按创建时间排降序
        //调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        //返回结果
        return Result.ok(pageHospitalSet);
    }

    @ApiOperation(value = "增加医院设置")
    @RequestMapping("save")
    public Result savehospot(@RequestBody HospitalSet hospitalSet){
        System.out.println(hospitalSet);
        //设置状态 0使用 1 不能使用
        hospitalSet.setStatus(0);
        //签名密钥
        Random random = new Random();
        //生成密钥规则：当前时间毫秒数 + 1000以内的随机数
        String signKey = MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000));
        hospitalSet.setSignKey(signKey.substring(4, 10));
        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save) {
            Note note = new Note();
            note.token(hospitalSet.getContactsPhone(), hospitalSet.getSignKey());
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "批量删除和删除医院数据")
    @PostMapping("delete")
    public Result delete(@RequestBody List<Long> arrId){
        System.out.println(arrId +"ssdasdasdasasdasd");
        hospitalSetService.removeByIds(arrId);
        return Result.ok();
    }

    @ApiOperation(value = "批量删除和删除医院数据")
    @PostMapping("{id}")
    public Result delete1(@PathVariable int id){

        hospitalSetService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "修改医院数据")
    @PostMapping("update")
    public Result update(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok("删除成功");
        }else {
            return Result.fail();
        }
    }
    @ApiOperation(value = "根据id查询医院数据")
    @PostMapping("getHospSet/{id}")
    public Result selectbyid(@PathVariable String id) throws YyghException {
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }
    @ApiOperation(value = "修改医院状态")
    @PostMapping("status/{id}/{status}")
    public Result status(@PathVariable long id,@PathVariable int status){
        HospitalSet hospitalSet=new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok("修改成功");
        }else {
            return Result.fail();
        }
    }

}
